****** Factor Measures prepared for export to Matlab for analysis
****** Robustness check with factor proxies taken as measures with highest correlations with the 3 personality traits of interest using the BFI - 2 questionnaire in the validation data


cd ""
clear 
set more off

use survey_vars


******************* Reducing number of categories to reduce number of parameters to be estimated in factor model

*********************** 1. Emotional Stability 

foreach var of varlist de_grat1 {
	gen temp=inlist(`var',1,2,3)
	replace temp=2 if inlist(`var',4)
	replace temp=3 if inlist(`var',5,6,7)

	tab temp `var'
	drop `var'
	rename temp `var'
	}
	
foreach var of varlist rebel2 {
	gen temp=inlist(`var',1,2)
	replace temp=2 if inlist(`var',3)
	replace temp=3 if inlist(`var',4,5)
	tab temp `var'
	drop `var'
	rename temp `var'
	}	



local cat stable2	stable4	stable5	stable7
foreach var of varlist `cat'  {
	gen b_`var'=`var'>2
	}
drop `cat'

local cat stable8-stable11
foreach var of varlist `cat'{
	gen b_`var'=`var'>3
	}
drop `cat'


foreach var of varlist posit3 posit4 {
	gen temp=inlist(`var',1)
	replace temp=2 if inlist(`var',2,3)
	replace temp=3 if inlist(`var',4)
	tab temp `var'
	drop `var'
	rename temp `var'
	}
	
local cat posit9 
foreach var of varlist `cat' {
	gen b_`var'=`var'>2
	}	
drop posit9 

local cat de_grat2  
foreach var of varlist `cat' {
	gen b_`var'=`var'>4
	}
	
********************** 2.Cognitive

gen temp=inlist(cognit2,1,2,3)
replace temp=2 if inlist(cognit2,4,5)
replace temp=3 if inlist(cognit2,6)
replace temp=4 if inlist(cognit2,7)
tab temp cognit2
drop cognit2
rename temp cognit2

********************** 3.Extraversion
local cat confident* cre4 
foreach var of varlist `cat'  {
	gen b_`var'=`var'>2
	}
drop `cat'

foreach var of varlist de_grat8 {
	gen temp=inlist(`var',1)
	replace temp=2 if inlist(`var',2,3)
	replace temp=3 if inlist(`var',4)
	replace temp=4 if inlist(`var',5,6)
	replace temp=5 if inlist(`var',7)
	tab temp `var'
	drop `var'
	rename temp `var'
	}


local cat ex9
foreach var of varlist `cat' {
	gen b_`var'=`var'>3
	}
drop `cat'



local cat rebel3 
foreach var of varlist `cat' {
	gen b_`var'=`var'>3
	}
drop `cat'

local cat posit8
foreach var of varlist `cat' {
	gen b_`var'=`var'>2
	}	
drop `cat'


********************** 4.Conscientiousness
***ex6 is added for R1 robustness in Conscientiousness - reverse orientation
replace ex6=-ex6
replace ex6=ex6+6

foreach var of varlist ex6 {
	gen temp=inlist(`var',1,2)
	replace temp=2 if inlist(`var',3)
	replace temp=3 if inlist(`var',4,5)
	tab temp `var'
	drop `var'
	rename temp `var'
	}

local cat dilligent1 dilligent3 
foreach var of varlist `cat' {
	gen b_`var'=`var'>3
	}

local cat dilligent5 dilligent8 dilligent10 dilligent11 dilligent12   
foreach var of varlist `cat' {
	gen b_`var'=`var'>4
	}

drop dilligent1 dilligent3 dilligent5 dilligent8 dilligent10 dilligent11 dilligent12  

foreach var of varlist dilligent* {
	gen temp=inlist(`var',1,2)
	replace temp=2 if inlist(`var',3,4,5)
	replace temp=3 if inlist(`var',6,7)
	tab temp `var'
	drop `var'
	rename temp `var'
	}


local cat de_grat4 de_grat5 de_grat6 de_grat7 de_grat10 de_grat11   
foreach var of varlist `cat' {
	gen b_`var'=`var'>4
	}

drop de_grat4 de_grat5 de_grat6 de_grat7  de_grat10 de_grat11 de_grat2

foreach var of varlist de_grat3 de_grat9 de_grat12 {
	gen temp=inlist(`var',1,2)
	replace temp=2 if inlist(`var',3,4,5)
	replace temp=3 if inlist(`var',6,7)
	tab temp `var'
	drop `var'
	rename temp `var'
	}




foreach var of varlist cre6 {
	gen temp=inlist(`var',1,2)
	replace temp=2 if inlist(`var',3)
	replace temp=3 if inlist(`var',4,5)
	tab temp `var'
	drop `var'
	rename temp `var'
	}
	







	
************************************* Now need to get in correct order for matlab estimator: organize by factor (binary, multi, continous in this order)


global id "subject"
*** sex=1=female
*** english=1=home language is english
*** age1=1=17
*** age2=1=18
*** age3=1=19+

global dems "sex english age17 age18 age19plus"
global cognit "cognit*"
global es_r1 "b_stable8	b_stable2	b_stable4	b_stable5	b_stable7	rebel2	b_posit9	b_de_grat2	posit3	posit4"
global extrav_r1 "de_grat8	b_cre4	ex4	b_rebel3	ex7	b_ex9	b_confident2	b_posit8	b_confident3	b_confident6"
global consci_r1 "b_dilligent5	b_dilligent10	b_de_grat7	b_dilligent11	dilligent6	dilligent9	ex6	de_grat12	cre6	b_de_grat5"
***********************************************************************************************************************************************



local j=0
foreach cat in dems es_r1 cognit extrav_r1 consci_r1  {
	local i=0
	local k=0
	local l=0
	foreach var of varlist $`cat' {
		quietly sum `var'
		*binary
		if `r(max)'==1 {
			local ++i
			* keeps track of which dummy became what var
			di "`var' _`j'`cat'_1b_`i'"
			rename `var' _`j'`cat'_1b_`i'
			}
		*multi-valued
		else if `r(max)'<=5 {
			* so that the 1st cat is at 0 - thats what the estimation code needs as input
			replace `var'=`var'-1
			local ++k
			rename `var' _`j'`cat'_2m_`k'
			di "`var' _`j'`cat'_2m_`k'"
			}	
		*continuous
		else {
			local ++l
			* standardizes the continuous variables to be on a similar scale as the rest
			replace `var' = (`var' - `r(mean)')/`r(sd)'
			rename `var' _`j'`cat'_3c_`l'
			di "var' _`j'`cat'_3c_`l'"
			}
		}
	di "`cat'"
	* counts the number of binary, multi-valued, and continuous indicators in each factor
	di "`i' `k' `l'"
	di `i'+`k'+`l'
	local ++j
	}

order  _all, sequential

* drops the unused variables
keep _*

outsheet using factor_data_R1, comma replace
save factor_data_R1, replace


